package com.example.chatgptapplication.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatgptapplication.entity.Admin;
import com.example.chatgptapplication.entity.Tag;
import com.example.chatgptapplication.entity.User;
import com.example.chatgptapplication.mapper.TagMapper;
import com.example.chatgptapplication.mapper.UserTagMapper;
import com.example.chatgptapplication.relation.UserTag;
import com.example.chatgptapplication.service.TagService;
import com.example.chatgptapplication.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {


    @Autowired
    private UserTagService userTagService;


    @Autowired
    private UserTagMapper userTagMapper;


    @Override
    public Page<Tag> listTagPage(Tag tag) {
        Page<Tag> adminDTOPage = getPageData(tag);
        QueryWrapper<Tag> queryWrapper = getQueryData(tag);
        return this.page(adminDTOPage,queryWrapper);
    }

    @Override
    @Transactional
    public void deleteTag(Tag tag) {
        //删除
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("tag_id",tag.getId());
        userTagService.remove(wrapper);
        removeById(tag.getId());
    }


    public Page<Tag> getPageData(Tag tag){
        Integer pageNum = tag.getPageNum();
        Integer pageSize = tag.getPageSize();
        Page<Tag> page = new Page<>(pageNum,pageSize);
        return page;
    }


    public QueryWrapper<Tag> getQueryData(Tag tag){
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        if(tag.getTagName() != null && !tag.getTagName().equals("")){
            queryWrapper.eq("tag_name",tag.getTagName());
        }
        return queryWrapper;
    }
}
